
#include Once "../inc/map.bi"
#include once "ext/graphics/img_load.bi"
#include once "ext/graphics.bi"

extern Gui(8) as fb.image ptr
extern curr_map as map
extern console as Uinteger
extern tiles as ext.gfx.sprite ptr


const as integer xoffset = 16 'for aligning the window to the center
                              ' since we have 32 'extra' pixels
const as integer yoffset = 12 '24 pixels for y
Dim As fb.image ptr Gui(8) 

function loadMap(file as string, byref ret_map as map) as integer

   dim as Integer f,flag = 0
   dim temp as string * 8
   Dim As UByte read_byte '1 byte
   Dim As UShort read_short ' 2 bytes
   Dim As integer read_int,read_size '4 bytes
   dim as string datfile = file

   f = freefile()
   var ret = open( file for binary access read as #f )
   
   ' Run time error?
   if ret <> 0 then
      print #console, "Error opening map file: '" & file & "' Err# " & ret
      Return ret
   EndIf
   
   ' Check the header first
   Get #f,,temp
   print #console, temp
   
   If Not(temp = MAP_HEADER) Then
      ?"Invalid Map header!"
      Close #f
      Return MAP_ERROR_BAD_HEADER
   End If
   

   ' start the reading
   While(flag = 0)
      ' get the descriptor first
      Get #f, ,read_int
      get #f, ,read_size
      print #console, "read_int: " & read_int
      print #console, "read_size: " & read_size
      
      Select Case (read_int)
         Case MAP_NAME, MAP_DESCRIPTION:
         
            Dim As zString ptr temp = Callocate( read_size + 1 )
            Get #f, ,*cptr(byte ptr, temp), read_size
            
            if read_int = MAP_NAME then
               ret_map.name = *temp
               print #console, ">> Map Name : " & ret_map.name
            else
               ret_map.desc = *temp
               print #console, ">> Map Desc : " & ret_map.desc
            end if

            DeAllocate(temp)  
                    
         Case MAP_DATA:
            
            ' Format: MAP_DATA SIZE X_COORD Y_COORD 
            
            print #console, ">> Map Data : "
         
            With ret_map
            
               Get #f,,read_byte 
               .max_x = read_byte 'x coord
               Get #f,,read_byte
               .max_y = read_byte 'y coord
               print #console, "Map size : (" & .max_x & "," & .max_y & ")"
            
               .grid = Callocate ((.max_x * SizeOf(map_block Ptr)) + 1)
               
               For i As Integer = 1 To .max_x
                  .grid[i] = CAllocate ((.max_y * SizeOf(map_block)) + 1)  
               Next i 
           
               For y As Integer = 1 To .max_y
               
                  For x As Integer = 1 To .max_x
                     
                     ' Thus for each Map_Block we'll have
                     ' Format: TILE_ID DESCRIPTOR
            
                     Get #f,,read_byte ' the tile id
                     .grid[x][y].id = read_byte
                     'Print #console,using "###"; read_byte & " " ;
                     Print #console,read_byte & " " ;

                     Get #f,,read_byte ' the descriptor
               
                     Select Case (read_byte)                  
                        Case MAP_DATA_TELEPORT
                           ' Format: MAP_DATA_TELEPORT TELE_X TELE_Y RESERVED RESERVED
                           
                           Get #f,,.grid[x][y].gp_x ' teleport x
                           Get #f,,.grid[x][y].gp_y ' teleport y
                           Get #f,,read_byte 'two reserved blocks
                           Get #f,,read_byte
                           
                        Case MAP_DATA_SWITCH
                           
                           ' Pending
                           
                        Case Else
                           ' Format: MAP_DATA_NULL
                           ' Do nothing
                                                      
                     End Select
                  Next x
                  Print
               Next y
               
            End With
         Case MAP_NUM_HEROES:
            ' Format:  Map_num_heroes No_of_heroes
            
            ret_map.default_heroes = read_size
            print #console, ">> Map Number of Heroes : " & read_size
            
         Case MAP_TILES_GFX:
            
            '----------------- Maybe In next version -----------------------
	
            '	Format: MAP_TILES_GFX	NO_OF_FILES	<LIST OF FILES>
            '           int            int         
            '  Where <LIST OF FILES> will contain individual descriptors as follows:

	         '  ENUM_MAP_TILES		SIZE		STRING
	         '  byte              int      string
            ''	The tile id	   size of path		the path to the file

            
      End Select
      
      If (Eof(f)) Then flag = 1
      
   wend
   
   Close #f
   
   datfile = left(datfile,instrrev(datfile,".")-1) & ".dat"
   
   f = freefile()
   
   dim as integer tileid
   dim as string tilepath
   
   open datfile for input as #f
      
      while(not(eof(f)))
         
         input #f,tileid,tilepath
         
         ret_map.tiles(tileid) = ext.LoadImage(tilepath)
         
         print #console, "Loading " & tilepath
         
         if (ret_map.tiles(tileid) = 0) then 
            print #console, "Error loading tile! : " & tileid & " " & tilepath
            print #console, err
            sleep
            end
         end if  

      wend     
   
   close #f
   
   Function = ret

end function

sub unloadMap(byref curr_map as map)

   print #console , "Unloading Map data..."
   With curr_map

      For i As Integer = 1 To .max_x
         if(.grid[i] <> 0 ) then DeAllocate (.grid[i])
      Next i
   
      if( .grid <> 0 ) then DeAllocate (.grid)
   
   End With
   
   
   /'print #console , "Unloading Map Tiles..."
   for i as integer = 0 to 255
      
      if(curr_map.tiles(i) <> 0) then imagedestroy(curr_map.tiles(i))
      
   next'/
   
end sub

sub drawMap(camx as integer, camy as integer)
   
   dim as integer x,y
   
   if((camx + 5) < curr_map.max_x) then 
      x = map_x
   else
      x = (map_x - 5)
   end if 
   
   if((camy + 6) < curr_map.max_y) then
      y = map_y
   else
      y = (map_y - 6)
   end if
   
   with curr_map
      

      
      for j as integer = -y to y '.max_y
         for i as integer = -x to x '.max_x
            
            'if( ((i + 6) < .max_x) AND ((j + 5) < .max_y)) then
               
               if(i + camx > 0) AND _
                  (j + camy > 0) AND _
                    (i + camx < .max_x) AND _ 
                     (j + camy < .max_y) then
                  put((i * Tile_Size) + xoffset, _
                     (j * Tile_Size) + yoffset _
                     ),.Tiles(.grid[i + camx][j + camy].id),pset
               end if
            'else
               /'put(((i -1) * Tile_Size) + xoffset, _
                  ((j -1) * Tile_Size) + yoffset _
                  ),imagecreate(48,48,rgb(0,0,0)),pset'/
            'end if
            
            
         next
      next
      
   end with   

end sub
